package com.seed.databasedesign.entity.table;

import lombok.Data;
import org.jeecg.modules.online.cgform.entity.OnlCgformField;
import org.jsoup.select.Elements;
import org.springframework.util.StringUtils;

import static com.seed.databasedesign.entity.table.TableColumnEnum.*;

/**
 * 列实体
 *
 * @author gchiaway
 * 日期: 2020-05-02
 * 时间: 22:33
 */
@Data
public class ColumnEntity {

    /**
     * 字段名称
     */
    private String fieldName;
    /**
     * 注释
     */
    private String content;
    /**
     * 字段类型
     */
    private String type;
    /**
     * 字段长度
     */
    private String length;
    /**
     * 字段默认值
     */
    private String fieldDefault;
    /**
     * 是否允许空值
     */
    private int isNull;
    /**
     * 校验必填
     */
    private String isMustInput;
    /**
     * 控件类型
     */
    private String fieldShowType;
    /**
     * 表单显示
     */
    private int isShowForm;
    /**
     * 列表显示
     */
    private int isShowList;
    /**
     * 是否排序
     */
    private String sortFlag;
    /**
     * 是否查询
     */
    private int isQuery;
    /**
     * 查询类型
     */
    private String queryMode;
    /**
     * 字典表
     */
    private String dictName;
    public ColumnEntity() {

    }
    public ColumnEntity(Elements tds) {
        this.fieldName = tds.get(NAME.getIndex()).text().replaceAll(" ", "");
        this.content = tds.get(TEXT.getIndex()).text().replaceAll(" ", "");
        this.type = tds.get(TYPE.getIndex()).text().replaceAll(" ", "");
        this.length = tds.get(LENGTH.getIndex()).text().replaceAll(" ", "");
        this.fieldDefault = tds.get(DEFAULT_VAL.getIndex()).text().replaceAll(" ", "");

        String isNullText = tds.get(IS_NULL.getIndex()).text().replaceAll(" ", "");
        if (StringUtils.isEmpty(isNullText)) {
            this.isNull = 1;
        } else {
            switch (isNullText) {
                case "Y":
                case "y": {
                    this.isNull = 1;
                    break;
                }
                case "N":
                case "n": {
                    this.isNull = 0;
                    break;
                }
                default: {
                    this.isNull = 0;
                }
            }
        }

        String isMustInputText = tds.get(IS_MUST_INPUT.getIndex()).text().replaceAll(" ", "");
        if (StringUtils.isEmpty(isMustInputText)) {
            this.isMustInput = "0";
        } else {
            switch (isMustInputText) {
                case "Y":
                case "y": {
                    this.isMustInput = "1";
                    break;
                }
                case "N":
                case "n": {
                    this.isMustInput = "0";
                    break;
                }
                default: {
                    this.isMustInput = "0";
                }
            }
        }

        String isShowFormText = tds.get(IS_SHOW_FORM.getIndex()).text().replaceAll(" ", "");
        if (StringUtils.isEmpty(isShowFormText)) {
            this.isShowForm = 1;
        } else {
            switch (isShowFormText) {
                case "Y":
                case "y": {
                    this.isShowForm = 1;
                    break;
                }
                case "N":
                case "n": {
                    this.isShowForm = 0;
                    break;
                }
                default: {
                    this.isShowForm = 1;
                }
            }
        }

        String isShowListText = tds.get(IS_SHOW_LIST.getIndex()).text().replaceAll(" ", "");
        if (StringUtils.isEmpty(isShowListText)) {
            this.isShowList = 1;
        } else {
            switch (isShowListText) {
                case "Y":
                case "y": {
                    this.isShowList = 1;
                    break;
                }
                case "N":
                case "n": {
                    this.isShowList = 0;
                    break;
                }
                default: {
                    this.isShowList = 1;
                }
            }
        }

        String sortFlagText = tds.get(SORT_FLAG.getIndex()).text().replaceAll(" ", "");
        if (StringUtils.isEmpty(sortFlagText)) {
            this.sortFlag = "0";
        } else {
            switch (sortFlagText) {
                case "Y":
                case "y": {
                    this.sortFlag = "1";
                    break;
                }
                case "N":
                case "n": {
                    this.sortFlag = "0";
                    break;
                }
                default: {
                    this.sortFlag = "0";
                }
            }
        }

        String isQueryText = tds.get(IS_QUERY.getIndex()).text().replaceAll(" ", "");
        if (StringUtils.isEmpty(isQueryText)) {
            this.isQuery = 0;
        } else {
            switch (isQueryText) {
                case "Y":
                case "y": {
                    this.isQuery = 1;
                    break;
                }
                case "N":
                case "n": {
                    this.isQuery = 0;
                    break;
                }
                default: {
                    this.isQuery = 0;
                }
            }
        }

        this.fieldShowType = tds.get(FIELD_SHOW_TYPE.getIndex()).text().replaceAll(" ", "");
        this.queryMode = tds.get(QUERY_MODE.getIndex()).text().replaceAll(" ", "");
        this.dictName = tds.get(DICT_NAME.getIndex()).text().replaceAll(" ", "");
    }

    public OnlCgformField toCgformField() {
        OnlCgformField cgFormField = new OnlCgformField();
        cgFormField.setDbFieldName(this.fieldName);
        cgFormField.setDbFieldTxt(this.content);

        switch (this.type) {
            case "integer":
            case "Integer":
            case "int":
            case "Int": {
                cgFormField.setDbType("int");
                break;
            }
            case "String":
            case "string": {
                cgFormField.setDbType("string");
                break;
            }
            case "Date":
            case "date": {
                cgFormField.setDbType("Date");
                break;
            }
            case "Datetime":
            case "datetime": {
                cgFormField.setDbType("Datetime");
                break;
            }
            case "Text":
            case "text": {
                cgFormField.setDbType("Text");
                break;
            }
            case "double":
            case "Double": {
                cgFormField.setDbType("double");
                break;
            }
            case "bigDecimal":
            case "BigDecimal": {
                cgFormField.setDbType("BigDecimal");
                break;
            }
            case "blob":
            case "Blob": {
                cgFormField.setDbType("Blob");
                break;
            }
            default: {
                cgFormField.setDbType("string");
            }
        }
        cgFormField.setDbIsKey(0);
        cgFormField.setDbIsNull(this.isNull);
        if (StringUtils.isEmpty(this.length)) {
            cgFormField.setDbLength(0);
            cgFormField.setDbPointLength(0);
        } else {
            if (this.length.contains(",")) {
                String[] lengthAndPointLength = this.length.split(",", 2);
                cgFormField.setDbLength(Integer.parseInt(lengthAndPointLength[0]));
                cgFormField.setDbPointLength(Integer.parseInt(lengthAndPointLength[1]));
            } else {
                cgFormField.setDbLength(Integer.parseInt(this.length));
                cgFormField.setDbPointLength(0);
            }
        }
        cgFormField.setDbDefaultVal(this.fieldDefault);
        if (StringUtils.isEmpty(this.fieldShowType)) {
            cgFormField.setFieldShowType("text");
        } else {
            cgFormField.setFieldShowType(this.fieldShowType);
        }
        if (!StringUtils.isEmpty(this.dictName)) {
            if (this.dictName.contains(",")) {
                String[] dicts = this.dictName.split(",");
                cgFormField.setDictTable(dicts[0]);
                cgFormField.setDictField(dicts[1]);
                cgFormField.setDictText(dicts[2]);
            } else {
                cgFormField.setDictField(this.dictName);
            }
        }
        cgFormField.setFieldLength(120);
        cgFormField.setFieldMustInput(this.isMustInput);
        cgFormField.setIsQuery(this.isQuery);
        if (this.isQuery == 0) {
            cgFormField.setQueryMode("single");
        } else {
            if (StringUtils.isEmpty(this.queryMode)) {
                cgFormField.setQueryMode("single");
            } else {
                cgFormField.setQueryMode(this.queryMode);
            }
        }
        cgFormField.setIsShowForm(this.isShowForm);
        cgFormField.setIsShowList(this.isShowList);
        cgFormField.setIsReadOnly(0);
        cgFormField.setQueryConfigFlag("0");
        if (StringUtils.isEmpty(this.sortFlag)) {
            cgFormField.setSortFlag("0");
        } else {
            cgFormField.setSortFlag(this.sortFlag);
        }
        return cgFormField;
    }

}
